<template>
  <div>
    <div class="pageheader">
      <XHeader :left-options="{backText:''}" :title="xheader.title" class="XHeader"></XHeader>
    </div>
    <div class="pagetop">
      <div class="topbar block">
        <div class="inputbox flex flex-center">
          <chimage class="icon" mode="aspectFit" :src="require('@/assets/img/ic_search.png')"></chimage>
          <input type="search" focus="true" style="text-align: left" v-model="keyword" class="input flex-full"
            placeholder="请输入搜索关键词" @keyup.enter="onSearch()">
          <div class="searchSubmit" @click="onSearch()">搜索</div>
        </div>
      </div>
    </div>

    <!--筛选排序-->
    <ScreenItem @onSearch="onSearch" :categoryList="categoryList" :from="from" :category_id="category_id" :height="showCateLabel ? '70px':'40px'"/>

    <scroller
      @on-pullup-loading="searchGoods()"
      lock-x
      ref="scroller"
      :pullup-config="{content:'',upContent:'',downContent:'释放加载更多',loadingContent:'加载中...',}"
      height="-45"
      :use-pullup="true"
      style=" padding-bottom:72px;"
      v-model="status"
    >
      <div class="page" :style="{'padding-top': showCateLabel ? '114px':'74px'}">
        <div class="block historybox" v-if="isHistorybox">
          <div class="title">历史搜索</div>
          <div class="historybar">
            <div class="item">商品</div>
            <div class="item">商品</div>
            <div class="item">商品</div>
            <div class="item">商品商品</div>
            <div class="item">商品</div>
            <div class="item">商品</div>
            <div class="item">商品</div>
            <div class="item">商品</div>
            <div class="item">商品</div>
          </div>
        </div>

        <div class="block" style="margin-top:12px;">
          <div class="grid grid-gutter discountbox">
            <div class="grid-1-2" v-for="(goods,inedx) in goodsList" :key="inedx">
              <div class="item" :class="[isHasGoodsText(goods) ? 'noStock' : '']" @click="onDetail(goods)">
                <div class="noStock-back" v-if="isHasGoodsText(goods)"></div>
                <div class="noStock-con">
                  <div class="supplier">
                    <chimage class="image" :src="goods.goods_image" mode="aspectFill"></chimage>
                    <div class="supplier-text">{{textSupplier(goods.supplier_id)}}</div>
                  </div>
                  <div class="noStock-tip" v-if="isHasGoodsText(goods)">
                    <div class="noStock-tip-text">无货</div>
                  </div>
                </div>
                <div class="title">{{goods.goods_name}}</div>
                <div class="list-price">
                  <div class="price newPrice">{{goods.price_alias}}￥{{goods.goods_price}}</div>
                  <div v-if="!isHasGoodsText(goods)" class="shop-btn"
                       @click.stop="onAddCart(goods.goods_id, goods.goods_sku, goods.goods_mininum)">
                    <img :src="require('@/assets/img/icon_item_gwc.png')"/>
                  </div>
                </div>

                <div class="list-price">
                  <div class="price oldPrice">{{goods.price_alias_market}}￥{{goods.goods_price_market}}</div>
                </div>

                <div class="list-price">
                  <div v-if="discountClass(goods.goods_price_discount)" class="discount"
                       :class="discountClass(goods.goods_price_discount)">
                    <chimage class="icon" :src="require('@/assets/img/icon_discount.png')"></chimage>
                    <span>{{goods.goods_price_discount}}%</span>
                  </div>
                </div>
              </div>
            </div>
          </div>
        </div>
        <load-more v-if="!more && !goodsList.length" :show-loading="false" :tip="'暂无数据'" background-color="#fbf9fe"></load-more>
      </div>
    </scroller>
  </div>
</template>

<script>
  import Mall from '@/dll/mall'
  import shop from '@/mixin/shop'
  import screen from '@/mixin/screen'
  import ScreenItem from '../../../components/screen'

  export default {
    name: 'mallSearch',
    mixins: [shop, screen],
    components: {
      ScreenItem
    },
    data () {
      return {
        xheader: {
          title: '搜索'
        },
        // 页面来源
        from: this.$route.query.from || undefined,
        // 搜索关键词
        keyword: '',
        // 搜索分类
        category_id: this.$route.query.category_id || undefined,
        // 搜索额度
        quota_id: this.$route.query.quota_id || undefined,
        // 搜索优惠券
        coupon_id: this.$route.query.coupon_id || undefined,
        // 搜索参数
        searchParams: {
          page: 1,
          page_size: 20
        },
        // 商品列表
        goodsList: [],
        // 二级分类列表
        showCateLabel: false,
        categoryList: [],
        // 是否显示历史搜索
        isHistorybox: false,
        // 是否还有更多
        more: true,
        // 下拉
        status: {
          pullupStatus: 'default'
        }
      }
    },
    created () {
      this.init()
    },
    mounted () {
      this.onHidden()
    },
    watch: {
    },
    methods: {
      init () {
        // 页面标题
        let title = this.$route.query.title
        if (title) {
          this.xheader.title = title
        }
        // 获取搜索商品
        this.searchGoods()
      },
      onDetail (item) {
        this.onAuto()
        this.$router.push({
          name: 'mallInfo',
          params: { goodsId: item.goods_id || 0 }
        })
      },
      onSearch (searchParams = {}) {
        this.searchParams.page = 1
        this.searchParams.category_id = searchParams.category_id
        this.searchParams.price = searchParams.price
        this.searchParams.sort_type = searchParams.sort_type
        this.searchParams.sort_desc = searchParams.sort_desc

        this.more = true
        this.goodsList = []
        this.status.pullupStatus = 'disabled'
        this.$nextTick(function () {
          this.$refs.scroller.enablePullup()
          this.$refs.scroller.reset({
            top: 0
          })
          this.status.pullupStatus = 'default'
        })
        this.searchGoods()
      },
      async searchGoods () {
        // 搜索关键词
        this.searchParams.keyword = this.keyword
        // 检索分类商品
        if (!this.searchParams.category_id) {
          this.searchParams.category_id = this.category_id
        }
        // 检索额度商品
        if (this.quota_id) {
          this.searchParams.quota_id = this.quota_id
        }
        // 检索优惠券商品
        if (this.coupon_id) {
          this.searchParams.coupon_id = this.coupon_id
        }
        let query_params = Object.assign({}, this.searchParams)

        // 地址信息
        let addressAreas = JSON.parse(localStorage.localSelectArea)
        query_params.area_code = addressAreas.areaCode

        this.$vux.loading.show({ text: '数据加载中...' })
        let res_goods = await Mall.getSearchList(query_params)
        this.$vux.loading.hide()
        if (res_goods.code == 0) {
          this.goodsList = [].concat(this.goodsList, res_goods.data.list)
          // 判断是否达到最后一页
          if (query_params.page >= res_goods.data.page_info.page_total) {
            this.more = false
            this.$refs.scroller.disablePullup()
          } else {
            this.$refs.scroller.donePullup()
          }
          this.searchParams.page = this.searchParams.page + 1

          // 获取二级分类列表
          if (res_goods.data.list.length) {
            if (this.from == 'index_category') {
              this.showCateLabel = true
              this.getCategoryList(this.category_id)
            } else {
              this.getCategoryAggs()
            }
          }
        } else {
          this.more = false
          this.$refs.scroller.disablePullup()
          this.$vux.alert.show({
            title: '提示',
            content: res_goods.msg
          })
        }
      },
      // 获取分类列表
      async getCategoryList (category_id) {
        let res_category = await Mall.getCategoryList({ category_id: category_id })
        if (res_category.code == 0) {
          if (!res_category.data.length) {
            this.showCateLabel = false
          }
          this.categoryList = res_category.data
        } else {
          this.showCateLabel = false
          this.$vux.alert.show({
            title: '提示',
            content: res_category.msg
          })
        }
      },
      // 获取分类商品聚合
      async getCategoryAggs () {
        let params = {
          keyword: this.keyword,
          coupon_id: this.coupon_id,
          quota_id: this.quota_id
        }
        let res_category = await Mall.getCategoryAggs(params)
        if (res_category.code == 0) {
          this.categoryList = res_category.data.count_category_goods
        } else {
          this.showCateLabel = false
          this.$vux.alert.show({
            title: '提示',
            content: res_category.msg
          })
        }
      }
    }
  }
</script>

<style lang="less" scoped>
  /deep/ .pageheader {
    z-index: 2 !important;
  }

  .page {
    padding-top: 84px;
  }

  .pagetop {
    display: block;
    width: 100%;
    position: fixed;
    top: 0;
    left: 0;
    z-index: 2;
    margin-top: 45px !important;
  }

  .topbar {
    box-sizing: border-box;
    background-color: #fff;
    padding-top: 5px;
    padding-bottom: 5px;
  }

  .topbar .icon {
    width: 20px;
    height: 20px;
    padding: 0 5px;
    display: block;
  }

  .topbar .inputbox {
    background: #fbf8f7;
    height: 35px;
  }

  .searchSubmit {
    height: 35px;
    line-height: 35px;
    font-size: 12px;
    border: none;
    background-color: transparent;
    color: #351009;
    width: 60px;
    text-align: center;
  }

  .topbar .input {
    font-size: 12px;
    color: #351009;
    text-align: center;
    line-height: 17px;
  }

  .historybox {
    position: fixed;
    width: 100%;
    top: 45px;
    left: 0;
  }

  .historybox .title {
    line-height: 20px;
    font-size: 14px;
    color: #351009;
  }

  .historybox .historybar {
    display: flex;
    flex-wrap: wrap;
    margin-left: -20px;
  }

  .historybox .historybar .item {
    display: block;
    height: 28px;
    font-size: 14px;
    color: #5b5b5b;
    line-height: 28px;
    padding-left: 34px;
    padding-right: 34px;
    background-color: #ffffff;
    border-radius: 14px;
    margin-left: 16px;
    margin-top: 16px;
  }

  .discountbox .item {
    display: block;
    background-color: #fff;
    padding: 12px;
    box-sizing: border-box;
  }

  .discountbox .item .image {
    display: block;
    height: auto;
    width: 100%;
    background-color: #fbf8f7;
    background-size: cover;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
  }

  .supplier {
    height: 145px;
    overflow: hidden;
  }

  .discountbox .item > .title {
    margin-top: 16px;
    font-size: 12px;
    color: #575757;
    line-height: 17px;
    height: 34px;
    text-overflow: ellipsis;
    overflow: hidden;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
  }

  .discountbox .item > .price {
    font-size: 14px;
    color: #ae2309;
    line-height: 16px;
  }
</style>
